class Solution(object):
    def matrixScore(self, grid):
        m = len(grid)
        n = len(grid[0])
        for i in range(m):
            if grid[i][0] == 0:
                for j in range(n):
                    grid[i][j] = 1 - grid[i][j]
        t = m * (1 << (n - 1))
        for j in range(1, n):
            base = 1 << (n - 1 - j)
            count_1 = sum([grid[i][j] for i in range(m)])
            c = max(count_1, m - count_1)
            t += base * c
        return t


data = Solution()
grid = [[0, 0, 1, 1], [1, 0, 1, 0], [1, 1, 0, 0]]
print(data.matrixScore(grid))
